package com.soulkun.smarthome.ui.main.home.viewmodel

import androidx.databinding.ObservableArrayList
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.soulkun.smarthome.logic.dto.*
import com.soulkun.smarthome.logic.model.DeviceModel
import com.soulkun.smarthome.logic.model.SpeechRecognitionResultModel
import com.soulkun.smarthome.logic.model.SpeechRecognitionSlotModel
import com.soulkun.smarthome.logic.repository.ApiRepository
import com.soulkun.smarthome.logic.vo.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import soulkun.library.mvvm.adapter.clearAndAddAll
import soulkun.library.utils.logE
import soulkun.library.utils.toast
import java.util.ArrayList

class AppMainHomeSpeechRecognitionViewModel : ViewModel() {

    val speechRecognitionStateLiveData = MutableLiveData<Int>(0) //0代表初始化 1代表正在识别 2代表识别成功 3代表识别出错
    val dataPrepareStateLiveData = MutableLiveData(0) //0代表初始化，1代表准备结束

    private val totalSpeechRecognitionCommandVOList = ArrayList<SpeechRecognitionCommandVO>()
    val roomNameSet = LinkedHashSet<String>()
    val deviceNameSet = LinkedHashSet<String>()
    val commandNameSet = LinkedHashSet<String>()

    val speechRecognitionCommandList = ObservableArrayList<SpeechRecognitionCommandVO>()

    fun resetViewModel() {
        speechRecognitionStateLiveData.value = 0
        dataPrepareStateLiveData.value = 0
        totalSpeechRecognitionCommandVOList.clear()
        roomNameSet.clear()
        deviceNameSet.clear()
        commandNameSet.clear()
        speechRecognitionCommandList.clear()
    }

    fun getLexiconSet(list: ArrayList<RoomDeviceCommandDTO>) {
        viewModelScope.launch(Dispatchers.Default) {
            val tempList = ArrayList<SpeechRecognitionCommandVO>()
            list.forEach { roomDeviceCommandDTO ->
                roomDeviceCommandDTO.deviceCommandList.forEach { deviceCommandDTO ->
                    deviceCommandDTO.commandList.forEach { commandDTO ->
                        val speechRecognitionCommandVO = SpeechRecognitionCommandVO(
                            roomDeviceCommandDTO.room.id!!,
                            roomDeviceCommandDTO.room.name!!,
                            deviceCommandDTO.device.id!!,
                            deviceCommandDTO.device.name!!,
                            deviceCommandDTO.device.productId!!,
                            commandDTO.id,
                            commandDTO.code,
                            commandDTO.description
                        )
                        tempList.add(speechRecognitionCommandVO)
                    }
                }
            }
            totalSpeechRecognitionCommandVOList.addAll(tempList)
            val roomNameSet =
                tempList.map(SpeechRecognitionCommandVO::roomName)
                    .toSet()
            val deviceNameSet =
                tempList.map(SpeechRecognitionCommandVO::deviceName)
                    .toSet()
            val commandNameSet =
                tempList.map(SpeechRecognitionCommandVO::commandDescription)
                    .toSet()
            this@AppMainHomeSpeechRecognitionViewModel.roomNameSet.addAll(roomNameSet)
            this@AppMainHomeSpeechRecognitionViewModel.deviceNameSet.addAll(deviceNameSet)
            this@AppMainHomeSpeechRecognitionViewModel.commandNameSet.addAll(commandNameSet)
            dataPrepareStateLiveData.postValue(1)
        }
    }

    fun filterSpeechRecognitionCommandList(
        speechRecognitionResultModel: SpeechRecognitionResultModel,
        onComplete: () -> Unit
    ) {
        viewModelScope.launch(Dispatchers.Default) {
            val list = when (speechRecognitionResultModel.slotList.size) { // 同时优先级：房间》设备》指令
                1 -> { // 相似推荐
                    when (speechRecognitionResultModel.slotList[0].slot) {
                        "<room>" -> {
                            totalSpeechRecognitionCommandVOList.filter {  //过滤到房间
                                speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<room>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.roomName } != null
                            }
                        }
                        "<device>" -> {
                            totalSpeechRecognitionCommandVOList.filter {  //过滤到房间
                                speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<device>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.deviceName } != null
                            }
                        }
                        else -> {
                            totalSpeechRecognitionCommandVOList.filter {  //过滤到房间
                                speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<command>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.commandDescription } != null
                            }
                        }
                    }
                }
                2 -> { // 相似推荐
                    val slotNameList =
                        speechRecognitionResultModel.slotList.map(SpeechRecognitionSlotModel::slot)
                    val rootFilterList = if (slotNameList.contains("<room>")) {
                        totalSpeechRecognitionCommandVOList.filter {
                            speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<room>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.roomName } != null
                        }
                    } else {
                        totalSpeechRecognitionCommandVOList.filter {
                            speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<device>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.deviceName } != null
                        }
                    }
                    if (slotNameList.contains("<device>")) {
                        rootFilterList.filter {
                            speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<device>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.deviceName } != null
                        }
                    } else {
                        rootFilterList.filter {
                            speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<command>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.commandDescription } != null
                        }
                    }
                }
                3 -> { // 第一次过滤确定
                    val roomFilterList = totalSpeechRecognitionCommandVOList.filter {  //过滤到房间
                        speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<room>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.roomName } != null
                    }
                    val deviceFilterList = roomFilterList.filter {  //过滤到设备
                        speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<device>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.deviceName } != null
                    }
                    val commandFilterList = deviceFilterList.filter { //过滤到指令
                        speechRecognitionResultModel.slotList.find { speechRecognitionSlotModel -> speechRecognitionSlotModel.slot == "<command>" }?.wordList?.find { speechRecognitionWordModel -> speechRecognitionWordModel.word == it.commandDescription } != null
                    }
                    if (commandFilterList.isNotEmpty()) { //第二次过滤确定
                        if (commandFilterList.size == 1) { //最终确定 设置为选择项
                            commandFilterList.also {
                                it[0].isSelected.set(true)
                            }
                        } else {
                            commandFilterList
                        }
                    } else {
                        if (deviceFilterList.isNotEmpty()) {
                            deviceFilterList
                        } else {
                            roomFilterList
                        }
                    }
                }
                else -> {
                    ArrayList<SpeechRecognitionCommandVO>()
                }
            }
            withContext(Dispatchers.Main) {
                speechRecognitionCommandList.clearAndAddAll(list)
                onComplete()
            }
        }
    }

    fun postDeviceCommand(familyId: Long, speechRecognitionCommandVO: SpeechRecognitionCommandVO) =
        ApiRepository.postDeviceCommand(
            DeviceCommandPostDTO(
                familyId,
                speechRecognitionCommandVO.deviceId,
                speechRecognitionCommandVO.productId,
                speechRecognitionCommandVO.commandId,
                0,
                60 * 1000
            )
        )

}